/**
 * @file opration.c
 * @brief   
 * @author  xiaowine (xiaowine@sina.cn)
 * @version 01.00
 * @date    2020-12-10
 * 
 * @copyright Copyright (c) {2020}  xiaowine
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2020-12-10 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */
#include "opration.h"

/**
	****************************************************************************
	* @Warning Without permission from the author,Not for commercial use
	* @File    readme.c
	* @Author  xiaowine@cee0.com
	* @date    
	* @version V1.0
	*************************************************
	* @brief   标注系统信息
	****************************************************************************
	* @attention 
	* Powered By Xiaowine
	* <h2><center>&copy;  Copyright(C) cee0.com 2015-2019</center></h2>
	* All rights reserved
	* 
**/

u8 HimDataBuff[50] = {0x00, 0x00, 0x00, 0x01};

ClkFg HIMReadClk = {25, 0};
ClkFg HIMWriteClk = {50, 0};
ClkFg HIMConnectFlashClk = {30, 0};
ClkFg SensorConnectFlashClk = {30, 0};
ClkFg ADCCaculateClk = {10, 0};

_uFLAG modbusFlag = {0};

#define SensorREGAddr 1
#define HimREGShowAddr 900
#define HimREGStart 950
#define HimREGStop 955
#define HimREGWarn 960
#define HimREGOnRange 965
#define HimREGOffRange 969
#define HimREGSetAddr 1000

u16 tempreture[5] = {0};
opration_t optData = {0};
u16 HimCommunicationCount = 0xffff;
u16 SensorCommunicationCount = 0xffff;
u16 timerCount = 0xffff;

u16 heaterZoneCount[4] = {0};

u16 HIMSetStatus = 0;

#define EEPROM_ADDRESS 32
#define EE_DATA_FLAG 0xa5a7

#define TEMPMAXHIGH (400 * 10)
#define TEMPMINLOW 0

#define SENSOROFFSET 1000
#define SENSORMAX 0XFFFF

void readDataEeData(void)
{
    if (ee_CheckOk() == 1)
    {
        u8 eeData[50] = {0};
        char cache[50] = {0};
        EEpExistFlag = 1;
        ee_ReadBytes(eeData, EEPROM_ADDRESS, 48);
        if (*((u16 *)&eeData[0]) == EE_DATA_FLAG)
        {
            sprintf(cache, "eep exist ,%04x\r\n", *((u16 *)&eeData[0]));
            comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
            memcpy(&optData, &eeData[2], sizeof(opration_t));
        }
        else
        {
            u8 i;
            sprintf(cache, "eep not exist,use default\r\n");
            comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
            for (i = 0; i < 5; i++)
            {
                optData.tempretureStart[i] = DEFAULT_START_TEMP;
                optData.tempretureStop[i] = DEFAULT_STOP_TEMP;
                optData.tempretureWarn[i] = DEFAULT_WARN_TEMP;
            }
            for (i = 0; i < 4; i++)
            {
                optData.heaterOnRange[i] = DEFAULT_ON_RANGE;
                optData.heaterOffRange[i] = DEFAULT_OFF_RANGE;
            }
        }
    }
    else
    {
        char cache[50] = {0};
        sprintf(cache, "eep not exist\r\n");
        comSendBuf(COM3, (uint8_t *)cache, strlen(cache));

        EEpExistFlag = 0;
    }
}

void writeDataEeData(void)
{
    if (DataNeedRefreshFlag)
    {
        u8 eeData[50] = {0};
        char cache[50] = {0};
        sprintf(cache, "eep write\r\n");
        comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
        memcpy(&eeData[2], &optData, sizeof(opration_t));
        DataNeedRefreshFlag = 0;
        *((u16 *)&eeData[0]) = EE_DATA_FLAG;
        ee_WriteBytes(eeData, EEPROM_ADDRESS, 48);
    }
}

/**
************************************************************************************************************************
 * @Function 	:		void ReadHIM(void)
 * @author		: 	xiaowine@cee0.com
 * @brief 		: 	读取屏转速值
 * @param 		: 	
 * @Return  	:		None
 * @????			:		
************************************************************************************************************************
*/
void ReadHIM(void)
{
    if (MODReadFlag)
    {
        MODReadFlag = 0;
        if (HIMSetStatus)
        {
            if (HIMSetStatus & 0x0001)
            {
                if (MODH_ReadParam_03H(SlaveHMIAddr, HimREGStart, 5) == 1)
                {
                    memcpy(optData.tempretureStart, g_tVar[SlaveHMIAddr - 1].P01, 10);
                    HIMSetStatus &= ~0x0001;
                    HimCommunicationCount = 0;
                }
                else
                {
                    char cache[50];
                    sprintf(cache, "read error\r\n");
                    comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
                }
                return;
            }
            if (HIMSetStatus & 0x0002)
            {
                if (MODH_ReadParam_03H(SlaveHMIAddr, HimREGStop, 5) == 1)
                {
                    memcpy(optData.tempretureStop, g_tVar[SlaveHMIAddr - 1].P01, 10);
                    HIMSetStatus &= ~0x0002;
                }
                else
                {
                    char cache[50];
                    sprintf(cache, "read error\r\n");
                    comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
                }
                return;
            }
            if (HIMSetStatus & 0x0004)
            {
                if (MODH_ReadParam_03H(SlaveHMIAddr, HimREGWarn, 5) == 1)
                {
                    memcpy(optData.tempretureWarn, g_tVar[SlaveHMIAddr - 1].P01, 10);
                    HIMSetStatus &= ~0x0004;
                    HimCommunicationCount = 0;
                }
                else
                {
                    char cache[50];
                    sprintf(cache, "read error\r\n");
                    comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
                }
                return;
            }
            if (HIMSetStatus & 0x0008)
            {
                if (MODH_ReadParam_03H(SlaveHMIAddr, HimREGOnRange, 4) == 1)
                {
                    memcpy(optData.heaterOnRange, g_tVar[SlaveHMIAddr - 1].P01, 8);
                    HIMSetStatus &= ~0x0008;
                    HimCommunicationCount = 0;
                }
                else
                {
                    char cache[50];
                    sprintf(cache, "read error\r\n");
                    comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
                }
                return;
            }
            if (HIMSetStatus & 0x0010)
            {
                if (MODH_ReadParam_03H(SlaveHMIAddr, HimREGOffRange, 4) == 1)
                {
                    memcpy(optData.heaterOffRange, g_tVar[SlaveHMIAddr - 1].P01, 8);
                    HIMSetStatus &= ~0x0010;
                    HimCommunicationCount = 0;
                    DataNeedRefreshFlag = 1;
                }
                else
                {
                    char cache[50];
                    sprintf(cache, "read error\r\n");
                    comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
                }
                return;
            }
        }
        else
        {
            if (MODH_ReadParam_03H(SlaveHMIAddr, HimREGSetAddr, 1) == 1)
            {
                // char cache[50];
                memcpy(&HIMSetStatus, g_tVar[SlaveHMIAddr - 1].P01, 2);
                HimCommunicationCount = 0;
                // sprintf(cache, "%d,%d,%d,%d\r\n", tempreture[0], tempreture[1], tempreture[2], tempreture[3]);
                // comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
            }
            else
            {
                char cache[50];
                sprintf(cache, "read error\r\n");
                comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
            }
        }
    }
    else
    {
        MODReadFlag = 1;
        if (MODH_ReadParam_04H(SlaveSensorAddr, SensorREGAddr, 4) == 1)
        {
            SensorCommunicationCount = 0;
            // char cache[50];
            memcpy(&tempreture[1], g_tVar[SlaveSensorAddr - 1].P01, 8);
            // sprintf(cache, "%d,%d,%d,%d\r\n", tempreture[0], tempreture[1], tempreture[2], tempreture[3]);
            // comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
        }
        else
        {
            char cache[50];
            sprintf(cache, "Sensor error\r\n");
            comSendBuf(COM3, (uint8_t *)cache, strlen(cache));
        }
    }
}
/**
************************************************************************************************************************
 * @Function 	:		void WriteHIM(void)
 * @author		: 	xiaowine@cee0.com
 * @brief 		: 	写入屏油门值
 * @param 		: 	
 * @Return  	:		None
 * @????			:		
************************************************************************************************************************
*/
void WriteHIM(void)
{
    u16 address = 0;
    u16 length = 0;
    if (!HMIOnLineFlag)
        return;
    if (MODWriteFlag)
    {
        MODWriteFlag = 0;
    }
    else
    {
        MODWriteFlag = 1;
    }
    // memset(HimDataBuff, 0, sizeof(HimDataBuff));
    if (!DataTransFlag)
    {
        address = 950;
        length = 23;
        HimDataBuff[0] = optData.tempretureStart[0] >> 8;
        HimDataBuff[1] = (u8)(optData.tempretureStart[0] & 0xff);
        HimDataBuff[2] = optData.tempretureStart[1] >> 8;
        HimDataBuff[3] = (u8)(optData.tempretureStart[1] & 0xff);
        HimDataBuff[4] = optData.tempretureStart[2] >> 8;
        HimDataBuff[5] = (u8)(optData.tempretureStart[2] & 0xff);
        HimDataBuff[6] = optData.tempretureStart[3] >> 8;
        HimDataBuff[7] = (u8)(optData.tempretureStart[3] & 0xff);
        HimDataBuff[8] = optData.tempretureStart[4] >> 8;
        HimDataBuff[9] = (u8)(optData.tempretureStart[4] & 0xff);

        HimDataBuff[10] = optData.tempretureStop[0] >> 8;
        HimDataBuff[11] = (u8)(optData.tempretureStop[0] & 0xff);
        HimDataBuff[12] = optData.tempretureStop[1] >> 8;
        HimDataBuff[13] = (u8)(optData.tempretureStop[1] & 0xff);
        HimDataBuff[14] = optData.tempretureStop[2] >> 8;
        HimDataBuff[15] = (u8)(optData.tempretureStop[2] & 0xff);
        HimDataBuff[16] = optData.tempretureStop[3] >> 8;
        HimDataBuff[17] = (u8)(optData.tempretureStop[3] & 0xff);
        HimDataBuff[18] = optData.tempretureStop[4] >> 8;
        HimDataBuff[19] = (u8)(optData.tempretureStop[4] & 0xff);

        HimDataBuff[20] = optData.tempretureWarn[0] >> 8;
        HimDataBuff[21] = (u8)(optData.tempretureWarn[0] & 0xff);
        HimDataBuff[22] = optData.tempretureWarn[1] >> 8;
        HimDataBuff[23] = (u8)(optData.tempretureWarn[1] & 0xff);
        HimDataBuff[24] = optData.tempretureWarn[2] >> 8;
        HimDataBuff[25] = (u8)(optData.tempretureWarn[2] & 0xff);
        HimDataBuff[26] = optData.tempretureWarn[3] >> 8;
        HimDataBuff[27] = (u8)(optData.tempretureWarn[3] & 0xff);
        HimDataBuff[28] = optData.tempretureWarn[4] >> 8;
        HimDataBuff[29] = (u8)(optData.tempretureWarn[4] & 0xff);

        HimDataBuff[30] = optData.heaterOnRange[0] >> 8;
        HimDataBuff[31] = (u8)(optData.heaterOnRange[0] & 0xff);
        HimDataBuff[32] = optData.heaterOnRange[1] >> 8;
        HimDataBuff[33] = (u8)(optData.heaterOnRange[1] & 0xff);
        HimDataBuff[34] = optData.heaterOnRange[2] >> 8;
        HimDataBuff[35] = (u8)(optData.heaterOnRange[2] & 0xff);
        HimDataBuff[36] = optData.heaterOnRange[3] >> 8;
        HimDataBuff[37] = (u8)(optData.heaterOnRange[3] & 0xff);

        HimDataBuff[38] = optData.heaterOffRange[0] >> 8;
        HimDataBuff[39] = (u8)(optData.heaterOffRange[0] & 0xff);
        HimDataBuff[40] = optData.heaterOffRange[1] >> 8;
        HimDataBuff[41] = (u8)(optData.heaterOffRange[1] & 0xff);
        HimDataBuff[42] = optData.heaterOffRange[2] >> 8;
        HimDataBuff[43] = (u8)(optData.heaterOffRange[2] & 0xff);
        HimDataBuff[44] = optData.heaterOffRange[3] >> 8;
        HimDataBuff[45] = (u8)(optData.heaterOffRange[3] & 0xff);
        if (MODH_WriteParam_10H(SlaveHMIAddr, address, length, HimDataBuff))
        {
            DataTransFlag = 1;
        }
        return;
    }
    if (HIMSetStatus & 0x1000)
    {
        HimDataBuff[0] = 0;
        HimDataBuff[1] = 0;
        if (MODH_WriteParam_10H(SlaveHMIAddr, HimREGSetAddr, 1, HimDataBuff))
        {
            HIMSetStatus &= ~0x1000;
        }
        return;
    }

    {
        address = HimREGShowAddr;
        length = 5;
        HimDataBuff[0] = tempreture[0] >> 8;
        HimDataBuff[1] = (u8)(tempreture[0] & 0xff);
        HimDataBuff[2] = tempreture[1] >> 8;
        HimDataBuff[3] = (u8)(tempreture[1] & 0xff);
        HimDataBuff[4] = tempreture[2] >> 8;
        HimDataBuff[5] = (u8)(tempreture[2] & 0xff);
        HimDataBuff[6] = tempreture[3] >> 8;
        HimDataBuff[7] = (u8)(tempreture[3] & 0xff);
        HimDataBuff[8] = tempreture[4] >> 8;
        HimDataBuff[9] = (u8)(tempreture[4] & 0xff);
        if (MODH_WriteParam_10H(SlaveHMIAddr, address, length, HimDataBuff))
        {
            address = 1;
        }
        else
        {
            address = 0;
        }
    }
}
/**
************************************************************************************************************************
 * @Function 	:		void SendCan(void)
 * @author		: 	xiaowine@cee0.com
 * @brief 		: 	屏转速发送给控制器
 * @param 		: 	
 * @Return  	:		None
 * @????			:		
************************************************************************************************************************
*/
void SendCan(void)
{
    //Can_Send_Msg(0x08, EEC1ID, EngineSpeedBuff, 8, CAN_ID_EXT, CAN_RTR_DATA); //发送8个字节
}
/**
************************************************************************************************************************
 * @Function 	:		void TimeOutOpration(void)
 * @author		: 	xiaowine@cee0.com
 * @brief 		: 	
 * @param 		: 	
 * @Return  	:		None
 * @????			:		
************************************************************************************************************************
*/
void TimeOutOpration(void)
{
    if (HimCommunicationCount < 200)
    {
        if (HIMConnectFlashClk.uFLAG == 1)
        {
            HIMConnectFlashClk.uFLAG = 0;
            PAout(2) ^= 1;
        }
        HMIOnLineFlag = 1;
    }
    else
    {
        HMIOnLineFlag = 0;
        PAout(2) = outPutOff;
    }
    if (SensorCommunicationCount < 200)
    {
        if (SensorConnectFlashClk.uFLAG == 1)
        {
            SensorConnectFlashClk.uFLAG = 0;
            PAout(3) ^= 1;
        }
        SensorOnLineFlag = 1;
    }
    else
    {
        memset(&tempreture[1], 0, 8);
        SensorOnLineFlag = 0;
        PAout(3) = outPutOff;
    }
    if (timerCount > 100)
    {
        u8 i;
        timerCount = 0;
        for (i = 0; i < 4; i++)
        {
            heaterZoneCount[i]++;
        }
    }
}
/**
************************************************************************************************************************
 * @Function 	:		void OprateCanRec(CanRxMsg *recCache)
 * @author		: 	xiaowine@cee0.com
 * @brief 		: 	处理CAN接收数据
 * @param 		: 	
 * @Return  	:		None
 * @????			:		
************************************************************************************************************************
*/
void OprateCanRec(CanRxMsg *RecCache)
{
    if (RecCache->IDE == CAN_Id_Extended)
    {
    }
}
